﻿/****************************** Module Header ******************************\
 * Module Name:  PageBasic.cs
 * Module Version:  1.0.0 (2011-12-15)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is a basic class for the webpage.  It supplies common public
 * methods to process a internet request, such as Request processing and so on. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UCF.Core.Web.Demo
{
        /// <summary>
        /// 登录帮助类(原名：LoginAuthentication)
        /// <para>版本：2.0.0 (2012-6-27)</para>
        /// <example>
        /// LoginHelper lh = new LoginHelper();
        /// 
        /// // Login Sample
        /// ItUserMember u;
        /// LoginResult lrv = BizFactory.BizUserMember.Login(tbLoginID.Text, tbPassword.Text, IPHelper.IPAddress, this.Domain, out u);
        /// if (lrv == LoginResult.Success)
        /// {
        ///     lh.AddLoginValues(u.ID.ToString(),u.LoginID,u.State.Value.ToString());
        ///     lh.Save();
        ///     Response.Redirect("/UCenter/Default.aspx");
        /// }
        /// else
        /// {
        ///     MessageBox.Show(this, lrv.ToText(), "");
        /// }
        /// 
        /// // Check Login
        /// 
        /// if (lh.CheckLogin("/Passport/Login.aspx"))
        /// {
        ///     this.passportUserID = lh.LoginInfo["id"].ToGuid();
        ///     this.passportUserName = lh.LoginInfo["loginid"];
        /// }
        /// </example>
        /// </summary>
        public class DemoLoginHelper
        {
            /// <summary>
            /// 登录信息名称
            /// </summary>
            private string Name;

            /// <summary>
            /// 记住信息名称
            /// </summary>
            private string RememberName;

            /// <summary>
            /// 登录信息数据
            /// </summary>
            public Dictionary<string, string> LoginInfo { get; set; }

            /// <summary>
            /// 记住信息数据
            /// </summary>
            public Dictionary<string, string> RememberInfo { get; set; }

            /// <summary>
            /// 构造方法
            /// </summary>
            /// <param name="isAdmin"></param>
            /// <param name="name"></param>
            /// <param name="remembername"></param>
            public DemoLoginHelper(bool isAdmin = false, string name = "login", string remembername = "remembername")
            {
                this.Name = name;
                this.RememberName = remembername;
                if (isAdmin)
                {
                    this.Name += "x";
                    this.RememberName += "x";
                }
                this.LoginInfo = this.Get(this.Name);
                this.RememberInfo = this.Get(this.RememberName);
                this.AddLoginFields("id", "loginid", "state", "sitetag");
                this.AddRememberFields("loginid", "password");
            }

            /// <summary>
            /// 获取数据
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            private Dictionary<string, string> Get(string name)
            {
                SessionDictionary sd = new SessionDictionary(name);
                return sd.DataSet;
            }

            /// <summary>
            /// 设置登录信息字段
            /// </summary>
            /// <param name="names"></param>
            public void AddLoginFields(params string[] names)
            {
                if (this.LoginInfo.Count > 0) return;
                this.LoginInfo.AddKeys(names);
            }

            /// <summary>
            /// 设置记住信息字段
            /// </summary>
            /// <param name="names"></param>
            public void AddRememberFields(params string[] names)
            {
                if (this.RememberInfo.Count > 0) return;
                this.RememberInfo.AddKeys(names);
            }

            /// <summary>
            /// 设置登录信息值
            /// </summary>
            /// <param name="values"></param>
            public void AddLoginValues(params string[] values)
            {
                this.LoginInfo.AddValues(values);
            }

            /// <summary>
            /// 设置记住信息值
            /// </summary>
            /// <param name="values"></param>
            public void AddRememberValues(params string[] values)
            {
                this.RememberInfo.AddValues(values);
            }

            /// <summary>
            /// 保存数据
            /// </summary>
            public void Save()
            {
                SessionDictionary sd = new SessionDictionary(this.Name);
                sd.DataSet = this.LoginInfo;
                sd.Save();
                SessionDictionary sd2 = new SessionDictionary(this.RememberName);
                sd2.DataSet = this.RememberInfo;
                sd2.Save();
            }

            /// <summary>
            /// 检测是否登录
            /// </summary>
            /// <param name="redirecturl">登录页面地址，如果不为空且未登录则自动跳转到该地址</param>
            /// <returns></returns>
            public bool CheckLogin(string redirecturl = null)
            {
                SessionDictionary sd = new SessionDictionary(this.Name);
                bool blnOut=false;
                foreach (string k in sd.DataSet.Keys)
                {
                    if (sd.DataSet[k].Length > 0)
                    {
                        blnOut = true;
                        break;
                    }
                }
                if (!blnOut)
                {
                    if (redirecturl != null) PageContext.Redirect(redirecturl);
                    return false;
                }
                return true;
            }

            /// <summary>
            /// 检测记住信息功能
            /// </summary>
            /// <param name="tbLoginID"></param>
            /// <param name="tbPassword"></param>
            /// <param name="chkRemember"></param>
            protected void CheckRemember(ref Control tbLoginID, ref Control tbPassword, ref Control chkRemember)
            {
                SessionDictionary sd = new SessionDictionary("loginRemember");
                if (sd.DataSet.Count == 0) return;
                tbLoginID.SetControlValue(sd.DataSet["loginid"]);
                tbPassword.SetControlValue(sd.DataSet["password"].SODEMDA());
                chkRemember.SetControlChecedState(true);
            }

            /// <summary>
            /// 注销信息
            /// </summary>
            public void LoginOut()
            {
                SessionDictionary sd = new SessionDictionary(this.Name);
                sd.Clear();
            }
        }
}
